<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>客服仪表板 - 工单管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --agent-primary: #28a745;
            --agent-secondary: #6c757d;
            --sidebar-width: 250px;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f8f9fa;
        }

        .sidebar {
            position: fixed;
            top: 0;
            left: 0;
            height: 100vh;
            width: var(--sidebar-width);
            background: linear-gradient(180deg, var(--agent-primary) 0%, #1e7e34 100%);
            color: white;
            z-index: 1000;
            transition: all 0.3s ease;
        }

        .sidebar.collapsed {
            width: 70px;
        }

        .sidebar-header {
            padding: 20px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            text-align: center;
        }

        .sidebar-header h4 {
            margin: 0;
            font-weight: 600;
        }

        .sidebar-header .collapse-btn {
            position: absolute;
            top: 20px;
            right: 15px;
            background: none;
            border: none;
            color: white;
            font-size: 18px;
            cursor: pointer;
        }

        .sidebar-menu {
            padding: 20px 0;
        }

        .menu-item {
            display: flex;
            align-items: center;
            padding: 12px 20px;
            color: rgba(255, 255, 255, 0.8);
            text-decoration: none;
            transition: all 0.3s ease;
            border-left: 3px solid transparent;
        }

        .menu-item:hover {
            background: rgba(255, 255, 255, 0.1);
            color: white;
            border-left-color: white;
        }

        .menu-item.active {
            background: rgba(255, 255, 255, 0.15);
            color: white;
            border-left-color: white;
        }

        .menu-item i {
            width: 20px;
            margin-right: 12px;
            text-align: center;
        }

        .menu-text {
            transition: opacity 0.3s ease;
        }

        .sidebar.collapsed .menu-text {
            opacity: 0;
            width: 0;
        }

        .main-content {
            margin-left: var(--sidebar-width);
            min-height: 100vh;
            transition: all 0.3s ease;
        }

        .sidebar.collapsed + .main-content {
            margin-left: 70px;
        }

        .top-navbar {
            background: white;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            padding: 15px 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .user-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: var(--agent-primary);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: 600;
        }

        .content-area {
            padding: 30px;
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .stat-card {
            background: white;
            border-radius: 15px;
            padding: 20px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            border-left: 4px solid var(--agent-primary);
            transition: transform 0.3s ease;
        }

        .stat-card:hover {
            transform: translateY(-5px);
        }

        .stat-card h3 {
            font-size: 2rem;
            font-weight: 700;
            margin: 0;
            color: var(--agent-primary);
        }

        .stat-card p {
            margin: 5px 0 0 0;
            color: #6c757d;
            font-weight: 500;
        }

        .stat-card i {
            font-size: 1.5rem;
            color: var(--agent-primary);
            margin-bottom: 10px;
        }

        .ticket-list {
            background: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            margin-bottom: 30px;
        }

        .ticket-item {
            display: flex;
            align-items: center;
            padding: 15px;
            border: 1px solid #e9ecef;
            border-radius: 10px;
            margin-bottom: 10px;
            transition: all 0.3s ease;
        }

        .ticket-item:hover {
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            transform: translateY(-2px);
        }

        .ticket-priority {
            width: 8px;
            height: 40px;
            border-radius: 4px;
            margin-right: 15px;
        }

        .ticket-priority.high {
            background: #dc3545;
        }

        .ticket-priority.medium {
            background: #ffc107;
        }

        .ticket-priority.low {
            background: #28a745;
        }

        .ticket-info {
            flex: 1;
        }

        .ticket-title {
            font-weight: 600;
            margin-bottom: 5px;
            color: #333;
        }

        .ticket-meta {
            font-size: 0.9em;
            color: #6c757d;
        }

        .ticket-status {
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 0.8em;
            font-weight: 600;
            text-transform: uppercase;
        }

        .ticket-status.open {
            background: #e3f2fd;
            color: #1976d2;
        }

        .ticket-status.in-progress {
            background: #fff3e0;
            color: #f57c00;
        }

        .ticket-status.pending {
            background: #f3e5f5;
            color: #7b1fa2;
        }

        .btn-agent {
            background: var(--agent-primary);
            border-color: var(--agent-primary);
            color: white;
        }

        .btn-agent:hover {
            background: #1e7e34;
            border-color: #1e7e34;
            color: white;
        }

        .btn-outline-agent {
            border-color: var(--agent-primary);
            color: var(--agent-primary);
        }

        .btn-outline-agent:hover {
            background: var(--agent-primary);
            border-color: var(--agent-primary);
            color: white;
        }

        .filter-tabs {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
        }

        .filter-tab {
            padding: 8px 16px;
            border: 1px solid #dee2e6;
            background: white;
            border-radius: 20px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 0.9em;
        }

        .filter-tab.active {
            background: var(--agent-primary);
            color: white;
            border-color: var(--agent-primary);
        }

        .filter-tab:hover:not(.active) {
            background: #f8f9fa;
        }

        @media (max-width: 768px) {
            .sidebar {
                transform: translateX(-100%);
            }

            .sidebar.show {
                transform: translateX(0);
            }

            .main-content {
                margin-left: 0;
            }

            .stats-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <!-- 侧边栏 -->
    <div class="sidebar" id="sidebar">
        <div class="sidebar-header">
            <h4><i class="fas fa-headset me-2"></i><span class="menu-text">客服工作台</span></h4>
            <button class="collapse-btn" onclick="toggleSidebar()">
                <i class="fas fa-bars"></i>
            </button>
        </div>
        <nav class="sidebar-menu">
            <a href="#" class="menu-item active" data-section="dashboard">
                <i class="fas fa-tachometer-alt"></i>
                <span class="menu-text">工作台</span>
            </a>
            <a href="#" class="menu-item" data-section="my-tickets">
                <i class="fas fa-ticket-alt"></i>
                <span class="menu-text">我的工单</span>
            </a>
            <a href="#" class="menu-item" data-section="assigned-tickets">
                <i class="fas fa-tasks"></i>
                <span class="menu-text">分配工单</span>
            </a>
            <a href="#" class="menu-item" data-section="feedbacks">
                <i class="fas fa-comments"></i>
                <span class="menu-text">客户反馈</span>
            </a>
            <a href="#" class="menu-item" data-section="customers">
                <i class="fas fa-users"></i>
                <span class="menu-text">客户管理</span>
            </a>
            <a href="#" class="menu-item" data-section="reports">
                <i class="fas fa-chart-bar"></i>
                <span class="menu-text">工作报告</span>
            </a>
        </nav>
    </div>

    <!-- 主内容区 -->
    <div class="main-content">
        <!-- 顶部导航栏 -->
        <div class="top-navbar">
            <div>
                <button class="btn btn-outline-secondary d-md-none" onclick="toggleSidebar()">
                    <i class="fas fa-bars"></i>
                </button>
                <h5 class="mb-0 d-none d-md-inline">客服工作台</h5>
            </div>
            <div class="user-info">
                <div class="dropdown">
                    <button class="btn btn-outline-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown">
                        <div class="user-avatar" id="userAvatar">A</div>
                        <span class="ms-2" id="userName">客服人员</span>
                    </button>
                    <ul class="dropdown-menu dropdown-menu-end">
                        <li><a class="dropdown-item" href="#" onclick="showProfile()">
                            <i class="fas fa-user me-2"></i>个人资料
                        </a></li>
                        <li><a class="dropdown-item" href="#" onclick="showSettings()">
                            <i class="fas fa-cog me-2"></i>设置
                        </a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item" href="#" onclick="logout()">
                            <i class="fas fa-sign-out-alt me-2"></i>退出登录
                        </a></li>
                    </ul>
                </div>
            </div>
        </div>

        <!-- 内容区域 -->
        <div class="content-area">
            <!-- 工作台概览 -->
            <div id="dashboard-section">
                <h2 class="mb-4">工作台概览</h2>
                
                <!-- 统计卡片 -->
                <div class="stats-grid">
                    <div class="stat-card">
                        <i class="fas fa-ticket-alt"></i>
                        <h3 id="myTickets">0</h3>
                        <p>我的工单</p>
                    </div>
                    <div class="stat-card">
                        <i class="fas fa-clock"></i>
                        <h3 id="pendingTickets">0</h3>
                        <p>待处理</p>
                    </div>
                    <div class="stat-card">
                        <i class="fas fa-check-circle"></i>
                        <h3 id="resolvedTickets">0</h3>
                        <p>已解决</p>
                    </div>
                    <div class="stat-card">
                        <i class="fas fa-comments"></i>
                        <h3 id="newFeedbacks">0</h3>
                        <p>新反馈</p>
                    </div>
                </div>

                <!-- 工单列表 -->
                <div class="ticket-list">
                    <div class="d-flex justify-content-between align-items-center mb-3">
                        <h5 class="mb-0">最近工单</h5>
                        <div class="filter-tabs">
                            <div class="filter-tab active" data-status="all">全部</div>
                            <div class="filter-tab" data-status="open">待处理</div>
                            <div class="filter-tab" data-status="in-progress">处理中</div>
                            <div class="filter-tab" data-status="pending">待回复</div>
                        </div>
                    </div>
                    <div id="ticketList">
                        <!-- 动态加载工单列表 -->
                    </div>
                </div>
            </div>

            <!-- 其他页面内容 -->
            <div id="other-sections" style="display: none;">
                <!-- 其他页面内容将在这里动态加载 -->
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 全局变量
        const API_BASE_URL = 'http://localhost:9090/api';
        let currentUser = null;
        let currentFilter = 'all';

        // 页面加载完成后初始化
        document.addEventListener('DOMContentLoaded', async () => {
            // 检查登录状态
            if (!await checkAuth()) {
                window.location.href = 'login.html';
                return;
            }

            // 加载用户信息
            await loadUserInfo();
            
            // 加载工作台数据
            await loadDashboardData();
            
            // 绑定事件
            bindEvents();
        });

        // 检查认证状态
        async function checkAuth() {
            const token = localStorage.getItem('token');
            if (!token) return false;

            try {
                const response = await fetch(`${API_BASE_URL}/auth/me`, {
                    headers: {
                        'Authorization': `Bearer ${token}`
                    }
                });

                if (response.ok) {
                    const data = await response.json();
                    if (data.success && data.data.role === 'AGENT') {
                        currentUser = data.data;
                        return true;
                    }
                }
            } catch (error) {
                console.error('认证检查失败:', error);
            }

            return false;
        }

        // 加载用户信息
        async function loadUserInfo() {
            if (currentUser) {
                document.getElementById('userName').textContent = currentUser.fullName || currentUser.username;
                document.getElementById('userAvatar').textContent = (currentUser.fullName || currentUser.username).charAt(0).toUpperCase();
            }
        }

        // 加载工作台数据
        async function loadDashboardData() {
            try {
                // 模拟数据
                document.getElementById('myTickets').textContent = '24';
                document.getElementById('pendingTickets').textContent = '8';
                document.getElementById('resolvedTickets').textContent = '156';
                document.getElementById('newFeedbacks').textContent = '12';

                // 加载工单列表
                loadTicketList();
            } catch (error) {
                console.error('加载工作台数据失败:', error);
            }
        }

        // 加载工单列表
        function loadTicketList() {
            const tickets = [
                {
                    id: 1,
                    title: '系统登录问题',
                    customer: '张三',
                    priority: 'high',
                    status: 'open',
                    created: '2小时前',
                    ticketNumber: '#T001234'
                },
                {
                    id: 2,
                    title: '数据导出功能异常',
                    customer: '李四',
                    priority: 'medium',
                    status: 'in-progress',
                    created: '4小时前',
                    ticketNumber: '#T001235'
                },
                {
                    id: 3,
                    title: '页面显示错误',
                    customer: '王五',
                    priority: 'low',
                    status: 'pending',
                    created: '6小时前',
                    ticketNumber: '#T001236'
                },
                {
                    id: 4,
                    title: '账户权限问题',
                    customer: '赵六',
                    priority: 'high',
                    status: 'open',
                    created: '1天前',
                    ticketNumber: '#T001237'
                }
            ];

            const container = document.getElementById('ticketList');
            container.innerHTML = tickets.map(ticket => `
                <div class="ticket-item" data-status="${ticket.status}">
                    <div class="ticket-priority ${ticket.priority}"></div>
                    <div class="ticket-info">
                        <div class="ticket-title">${ticket.ticketNumber} - ${ticket.title}</div>
                        <div class="ticket-meta">
                            客户：${ticket.customer} · ${ticket.created}
                        </div>
                    </div>
                    <div class="ticket-status ${ticket.status}">${getStatusText(ticket.status)}</div>
                    <div class="ms-3">
                        <button class="btn btn-sm btn-outline-agent" onclick="viewTicket(${ticket.id})">
                            <i class="fas fa-eye"></i>
                        </button>
                        <button class="btn btn-sm btn-agent ms-1" onclick="handleTicket(${ticket.id})">
                            <i class="fas fa-edit"></i>
                        </button>
                    </div>
                </div>
            `).join('');
        }

        // 获取状态文本
        function getStatusText(status) {
            const statusMap = {
                'open': '待处理',
                'in-progress': '处理中',
                'pending': '待回复',
                'resolved': '已解决',
                'closed': '已关闭'
            };
            return statusMap[status] || status;
        }

        // 绑定事件
        function bindEvents() {
            // 菜单事件
            const menuItems = document.querySelectorAll('.menu-item');
            menuItems.forEach(item => {
                item.addEventListener('click', (e) => {
                    e.preventDefault();
                    
                    menuItems.forEach(mi => mi.classList.remove('active'));
                    item.classList.add('active');
                    
                    const section = item.dataset.section;
                    handleMenuClick(section);
                });
            });

            // 筛选标签事件
            const filterTabs = document.querySelectorAll('.filter-tab');
            filterTabs.forEach(tab => {
                tab.addEventListener('click', () => {
                    filterTabs.forEach(t => t.classList.remove('active'));
                    tab.classList.add('active');
                    
                    currentFilter = tab.dataset.status;
                    filterTickets();
                });
            });
        }

        // 处理菜单点击
        function handleMenuClick(section) {
            if (section === 'dashboard') {
                document.getElementById('dashboard-section').style.display = 'block';
                document.getElementById('other-sections').style.display = 'none';
            } else {
                document.getElementById('dashboard-section').style.display = 'none';
                document.getElementById('other-sections').style.display = 'block';
                loadPageContent(section);
            }
        }

        // 加载页面内容
        function loadPageContent(section) {
            const container = document.getElementById('other-sections');
            const titles = {
                'my-tickets': '我的工单',
                'assigned-tickets': '分配工单',
                'feedbacks': '客户反馈',
                'customers': '客户管理',
                'reports': '工作报告'
            };
            container.innerHTML = `<h2>${titles[section] || '未知页面'}</h2><p>此功能正在开发中...</p>`;
        }

        // 筛选工单
        function filterTickets() {
            const ticketItems = document.querySelectorAll('.ticket-item');
            ticketItems.forEach(item => {
                const status = item.dataset.status;
                if (currentFilter === 'all' || status === currentFilter) {
                    item.style.display = 'flex';
                } else {
                    item.style.display = 'none';
                }
            });
        }

        // 查看工单
        function viewTicket(ticketId) {
            alert(`查看工单 #${ticketId} 的详细信息`);
        }

        // 处理工单
        function handleTicket(ticketId) {
            alert(`处理工单 #${ticketId}`);
        }

        // 切换侧边栏
        function toggleSidebar() {
            const sidebar = document.getElementById('sidebar');
            sidebar.classList.toggle('collapsed');
        }

        // 显示个人资料
        function showProfile() {
            alert('个人资料功能正在开发中...');
        }

        // 显示设置
        function showSettings() {
            alert('设置功能正在开发中...');
        }

        // 退出登录
        function logout() {
            if (confirm('确定要退出登录吗？')) {
                localStorage.clear();
                window.location.href = 'login.html';
            }
        }
    </script>
</body>
</html>
